<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once "./_inc/funcFunction.php";

$errMsg = "";
if(sxREQ('reqtype') == "save"){
	$errMsg .= functionIns();
}
sxRDef('upgrade_status', 'S');
for($kX=1;$kX<=5;$kX+=1) {
	sxRDef('variable_name_'.$kX,'');
	sxRDef('variable_desc_'.$kX,'');
	sxRDef('variable_type_'.$kX,'');
}

$aOpz = array("kMenu"=>'pro.fun', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
<?php
echo(bodyHTML($aOpz));

echo getLibHeader(sxREQ('lbr'),true);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>"save",
										"text"=>"save"
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);
echo $sCmd;	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('lbr');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>Function</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width='15%'>Name*</td>" .
			"<td class=\"text01\" colspan='3' width='85%'>@".getHtmlText('function',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\" colspan='3'>".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Language*</td>" .
			"<td class=\"text01\">");
echo getHtmlSelect('language', getLanguages());
echo("</td>" .
			"<td class=\"labl01\">Category*</td>" .
			"<td class=\"text01\">".getHtmlText('category',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Result-type*</td>" .
			"<td class=\"text01\">");
echo getHtmlSelect('result_type', getFuncType());
echo("</td>" .
			"<td class=\"labl01\">Upgrade status*</td>" .
			"<td class=\"text01\">".getHTMLSelect('upgrade_status', getUpgradeStatusVal())."</td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");

echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='4'>Parameters</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"1%\">&nbsp;</td>" .
			"<td class=\"labl01\" width=\"20%\">Name</td>" .
			"<td class=\"labl01\" width=\"10%\">Type</td>" .
			"<td class=\"labl01\" width=\"69%\">Description</td>" .
		"</tr>\n");
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,14)!='variable_name_') continue;
	$kX = substr($nX,14);
	echo("<tr>" .
			"<td class=\"text01\" align='right'>&nbsp;$kX)&nbsp;</td>" .
			"<td class=\"text01\">".getHtmlText('variable_name_'.$kX,32)."</td>" .
			"<td class=\"text01\">".getHtmlSelect('variable_type_'.$kX,getFuncType())."</td>" .
			"<td class=\"text01\">".getHtmlText('variable_desc_'.$kX,60)."</td>" .
		"</tr>\n");
}
echo("</table>");

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"50%\">Script</td>" .
			"<td class=\"titl01\" width=\"50%\">Note</td>" .
"</tr>\n");
echo("<tr>" .
			"<td class=\"text01\">".getHtmlTextArea('script',100,20)."</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',30,20)."</td>" .
"</tr>\n");
echo("</table>");

echo $sCmd;
echo("</form>");
echo(footerHTML());
exit();

function functionIns($redirOnEnd = true){
	global $objSDAO;
	$errMsg = "";
	$_REQUEST['function'] = strtolower(sxRTrim("function"));
	if(sxREQ("function") == '')
		$errMsg .= "missing name<br>";
	if(preg_match("/[^a-z0-9_]/",sxREQ("function")))
		$errMsg .= "Name: letter, numbers and _ only<br>";
	if(trim(sxREQ("description")) == '')
		$errMsg .= "missing description<br>";
	//existence
	$sSQL = "SELECT * FROM function WHERE function='".sxRAS("function")."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row)
		$errMsg .= "existing name - library '".$row['library']."'<br>";
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,14)!='variable_name_') continue;
		if (trim($vX)=='') continue;
		$kX = substr($nX,14);
		if(preg_match("/[^a-z0-9_]/",$vX))
			$errMsg .= "Parameter $kX: letter, numbers and _ only<br>";
	}	
	if($errMsg != ""){return $errMsg;}		
	$errMsg = checkScript();
	if($errMsg != ""){return $errMsg;}	
	
	$sSQL = "INSERT INTO " .
			"function " .
			"(function, " .
			"description, " .
			"category, " .
			"library, " .
			"language, " .
			"script, " .
			"result_type, " .
			"upgrade_status, " .
			"note)" .
			"VALUES('".sxRAS("function")."', " .
			"'".sxRAS("description")."', " .
			"'".sxRAS("category")."', " .
			"'".sxRAS("lbr")."', " .
			"'".sxRAS("language")."', " .
			"'".sxRAS("script")."', " .
			"'".sxRAS("result_type")."', " .
			"'".sxRAS("upgrade_status")."', " .
			"'".sxRAS("note")."'" .
			")";	
	$objSDAO->query($sSQL,"ins");
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,14)!='variable_name_') continue;
		if (trim($vX)=='') continue;
		$kX = substr($nX,14);
		$sSQL = "INSERT INTO function_variable (function, sequence, name, variable_type, description) " 
			." VALUES('".sxRAS("function")."',$kX,'".sxRAS("variable_name_".$kX)."'"
			.",'".sxRAS("variable_type_".$kX)."','".sxRAS("variable_desc_".$kX)."')";
		$objSDAO->query($sSQL,"ins");
	}	
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'abnormal end';
}
